package com.diandi.signature.controller.contract;


import com.diandi.signature.contract.SignatureContractService;
import com.diandi.signature.enums.fdd.ReturnCode;
import com.diandi.signature.enums.platfrom.SysModelType;
import com.diandi.signature.properties.FddProperties;
import com.diandi.signature.properties.SignatureProperties;
import com.diandi.signature.sign.SignUtil;
import com.diandi.signature.sign.Tools;
import com.diandi.signature.url.CreateTradeNo;
import com.diandi.signature.utils.CheckParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 合同签章信息查询
 * @author mwz
 * @create 2017-08-23 10:19
 * @version V1.0.0
 */
@Controller
@RequestMapping("/signatureContract")
public class SignatureQueryController {

    private static Logger logger = LoggerFactory.getLogger(SignatureQueryController.class);

    @Autowired
    private SignatureProperties signatureProperties;
    @Autowired
    private FddProperties fddProperties;
    @Autowired
    private SignatureContractService signatureContractService;


    /**
     * 合同信息查询
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/queryContract")
    @ResponseBody
    public String queryContract(HttpServletRequest request, HttpServletResponse response) {
        /**
         * 1.1 接收业务端发送来的消息
         */
        Map<String,String> returnMap =  new HashMap<String,String>();
        String platTradeNo = CreateTradeNo.createTradeNo("QZ");

        Long sp1 = System.currentTimeMillis();
        logger.info("--["+platTradeNo+"] queryContract Start--");

        logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同查询]请求消息，开始处理--");
        Map<String, String> params = Tools.getParameterMap(request, false);
        try {
            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同查询]requestMap:"+request.getRequestURL()+params+"--");
            //returnMap = CheckParams.checkParamsIsExist(params, "userId","merchantNo","passCode","contractNo","sign");
            returnMap = CheckParams.checkParamsIsExist(params, "userId","merchantNo","passCode","contractNo");

            Long sp2 = System.currentTimeMillis();
            logger.info("--["+platTradeNo+"] step1 use times:"+(sp2-sp1)+"--");

            if (null != returnMap && !returnMap.isEmpty()) {
                logger.info("收到业务平台[合同查询]请求消息其中参数不能为空,返回给业务端,结束处理");
//                returnMap = Tools.createReturnParams(ReturnCode.err_1004,ReturnCode.err_1004.getMsg(),null);
                return Tools.createBusinessLinkString(returnMap,true,signatureProperties.getCharset());
            }
            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同查询]: 校验业务平台参数正确--");

            Long sp3 = System.currentTimeMillis();
            logger.info("--["+platTradeNo+"] step2 use times:"+(sp3-sp2)+"--");
            /**
             * 1.2 查询相关数据
             */
            returnMap = signatureContractService.QueryContract( request,platTradeNo);

            //生成签名
            String sign = SignUtil.signRSA(returnMap, signatureProperties.getPayPrivateSign(params.get("merchantNo")), signatureProperties.getCharset());
            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同查询]: 生成签名完成--");
            returnMap.put("sign",sign);

            Long sp4 = System.currentTimeMillis();
            logger.info("--["+platTradeNo+"] step3 use times:"+(sp4-sp3)+"--");
        } catch (Exception e) {
            logger.error(e.toString());
            e.printStackTrace();
            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同查询]: 处理错误中断，返回:"+e.toString()+"--");
            returnMap = Tools.createReturnParams(ReturnCode.err,ReturnCode.err.getMsg(),null);
        }
        logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同查询]返回信息，结束处理--");
        logger.info("--["+platTradeNo+"] End queryContract , TotalTimes:"+(System.currentTimeMillis()-sp1)+"--");
        return Tools.createLinkString(returnMap,true);
    }
}